package com.youjiang.meeting.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.youjiang.meeting.mapper.ExpireMapper;
import com.youjiang.meeting.mapper.ProgramMapper;
import com.youjiang.meeting.mapper.SaveQrUrlMapper;
import com.youjiang.meeting.mapper.UserInfoMapper;
import com.youjiang.meeting.mapper.WxconfigMapper;
import com.youjiang.meeting.po.Expire;
import com.youjiang.meeting.po.ExpireExample;
import com.youjiang.meeting.po.Program;
import com.youjiang.meeting.po.ProgramExample;
import com.youjiang.meeting.po.SaveQrUrl;
import com.youjiang.meeting.po.SaveQrUrlExample;
import com.youjiang.meeting.po.UserInfo;
import com.youjiang.meeting.po.UserInfoExample;
import com.youjiang.meeting.po.Wxconfig;
import com.youjiang.meeting.po.WxconfigExample;
import com.youjiang.meeting.service.UserService;
import com.youjiang.meeting.utils.Md5Pwd;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserInfoMapper uMapper;
	@Autowired
	private ExpireMapper eMapper;
	@Autowired
	private ProgramMapper pMapper;
	@Autowired 
	private WxconfigMapper wMapper;
	@Autowired
	private SaveQrUrlMapper sqMapper;

	@Override
	public boolean register(String email, String password, String name) {
		// TODO Auto-generated method stub
		UserInfo userInfo = new UserInfo();
		
		userInfo.setEmail(email);
		userInfo.setName(name);
		userInfo.setOverage(0L);
		String md5Password = Md5Pwd.encode(password);
		userInfo.setPassword(md5Password);
		try {
			uMapper.insert(userInfo);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	@Override
	public UserInfo login(String mail, String password) {
		System.out.println("mail:"+mail);
		System.out.println("password:"+password);
		String loginMD5Pass = Md5Pwd.encode(password);
		// TODO Auto-generated method stub
		UserInfoExample example = new UserInfoExample();
		UserInfoExample.Criteria criteria = example.createCriteria();
		criteria.andEmailEqualTo(mail);
		criteria.andPasswordEqualTo(loginMD5Pass);
		List<UserInfo> infoList = uMapper.selectByExample(example);
		System.out.println("infoList---"+infoList.size());
		if(infoList.size()>0 && infoList!=null){
			
			return infoList.get(0);
			
		}
		return null;
	}

	@Override
	public boolean findByEmail(String mail) {
		// TODO Auto-generated method stub
		
		UserInfoExample example = new UserInfoExample();
		UserInfoExample.Criteria criteria = example.createCriteria();
		criteria.andEmailEqualTo(mail);
		List<UserInfo> infoList = uMapper.selectByExample(example);
		if(infoList!=null && infoList.size()>0){
			return false;
		}
		return true;
	}

	@Override
	public void insertResgisterTime(int userid, String time) {
		// TODO Auto-generated method stub
		Expire record = new Expire();
		record.setUserid(userid);
		record.setExpireIn(time);
		eMapper.insert(record);
	}

	@Override
	public UserInfo findUserInfoByEmail(String email) {
		// TODO Auto-generated method stub
		UserInfoExample example = new UserInfoExample();
		UserInfoExample.Criteria criteria = example.createCriteria();
		criteria.andEmailEqualTo(email);
		List<UserInfo> infos = uMapper.selectByExample(example);
		if(infos!=null && infos.size()>0){
			return infos.get(0);
		}
		return null;
	}

	@Override
	public Expire getExpireInByUserId(int id) {
		// TODO Auto-generated method stub
		ExpireExample example = new ExpireExample();
		ExpireExample.Criteria criyeria = example.createCriteria();
		criyeria.andUseridEqualTo(id);
		List<Expire> eList = eMapper.selectByExample(example);
		if(eList!=null && eList.size()>0){
			return eList.get(0);
		}
		return null;
	}

	@Override
	public List<Program> getProgramsByUserId(int id) {
		ProgramExample example  = new ProgramExample();
		ProgramExample.Criteria criteria = example.createCriteria();
		criteria.andUseridEqualTo(id);
		List<Program> activities = pMapper.selectByExample(example);
		if(activities!=null && activities.size()>0){
			return activities;
		}
		return null;
	}

	@Override
	public Wxconfig getWxconfigByUserId(int id) {
		// TODO Auto-generated method stub
		WxconfigExample example = new WxconfigExample();
		WxconfigExample.Criteria criteria = example.createCriteria();
		criteria.andUseridEqualTo(id);
		List<Wxconfig> wxconfigList = wMapper.selectByExample(example);
		if(wxconfigList!=null && wxconfigList.size()>0){
			return wxconfigList.get(0);
		}
		return null;
	}

	@Override
	public void writeConfigure(Wxconfig config) {
		// TODO Auto-generated method stub
		wMapper.insert(config);
	}

	@Override
	public void updateConfigure(Wxconfig config) {
		// TODO Auto-generated method stub
		wMapper.updateByPrimaryKey(config);
	}

	@Override
	public UserInfo findUserByUserId(Integer userid) {
		// TODO Auto-generated method stub
		UserInfo info = uMapper.selectByPrimaryKey(userid);
		if(info!=null){
			return info;
		}
		return null;
	}

	@Override
	public void updateResgisterTime(Integer id, String time) {
		// TODO Auto-generated method stub
		Expire record = new Expire();
		record.setUserid(id);
		record.setExpireIn(time);
		eMapper.updateByPrimaryKey(record);
	}

	@Override
	public SaveQrUrl getQrUrlFromUserid(int userid) {
		// TODO Auto-generated method stub
		SaveQrUrlExample example = new SaveQrUrlExample();
		SaveQrUrlExample.Criteria criteria = example.createCriteria();
		criteria.andUseridEqualTo(userid);
		List<SaveQrUrl> qrList = sqMapper.selectByExample(example);
		if(qrList!=null && qrList.size()>0){
			SaveQrUrl url = qrList.get(0);
			return url;
		}
		return null;
	}

}
